<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\BaseController;
use App\Http\Requests\Auth\LoginRequest;
use Illuminate\Http\Request;

class LoginController extends BaseController
{
    /**
     * 登录
     */
    public function login(LoginRequest $request)
    {
        $credentials = request(['email', 'password']);

        if (!$token = auth('api')->attempt($credentials)) {
            return $this->response->errorUnauthorized();
        }

        $user = auth('api')->user();
        if($user->is_locked == 1){
            return $this->response->errorForbidden('被锁定');
        }

        return $this->respondWithToken($token);
    }

    /**
     * 获取个人信息
     */
    public function me()
    {
        return response()->json(auth('api')->user());
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        auth('api')->logout();

        return $this->response()->noContent();
    }

    /**
     * 刷新token
     */
    public function refresh()
    {
        return $this->respondWithToken(auth('api')->refresh());
    }

    /**
     * 格式化返回
     */
    protected function respondWithToken($token)
    {
        return $this->response()->array([
                                            'access_token' => $token,
                                            'token_type'   => 'Bearer',
                                            'expires_in'   => auth('api')->factory()->getTTL() * 6000,
                                            //'expires_in'   => 10,
                                        ]);
    }
}
